#!/bin/bash
#Automated process to import data from a BAM10/BAM10s AS OPERATED BY CARB into DMS.
use()
{
	echo "Proper use:"
	echo "*	This script should be run from the CARBLog directory" 
	echo "$0 \"/path/to/input/file\""
	exit 1
}
if [ $# == 0 ]; then
	use
fi

if ! [ -f "$1" ]; then
	echo "Sorry, I can't access the file \" $1 \"..."
	use
fi
#GRAB LOCAL CONFIGURATIONS FOR SITE CODE, ETC
while read line; do
    eval $line
done < Config/config
#INITIALIZE THIS VARIABLE
this_value=''
#CREATE FILE NAME
this_file="${saroad}_backpoll.MIN"
#BLANK THE FILE IN CASE IT ALREADY EXISTS
echo '' >$this_file
#FEEDBACK THE SITE CODE WE ARE PROCESSING
echo "Processing site: ${saroad}"
#DETERMINE ALL POSSIBLE BAMS WE COULD BE USING AT THE MOMENT, THEN ASK THE USER IF THIS IS THE ONE
possible_instruments=`cat Config/config | grep -o [A-Z]*BAM[0-9,A-Z,a-z]*_drv`
for i in ${possible_instruments}; do
	echo "Is $i the instrument that this data is coming from?"
	read response
	if [[ "$response" == [Y,y]* ]]; then
		echo "Processing $i..."
		#VALIDATE THAT THE USER CHOSE THE CORRECT INSTRUMENT BY GRABBING THE HEADERS
	    echo "grabbing headers from instrument..."
		inst_port=`cat resources/${i}/.usr1 | grep -oe "-d /dev/tty[A-Z]*[0-9]" | sed 's/-d //g'`
		echo "$i port is $inst_port"
		sleep 5
		inst_baud=`cat resources/${i}/.usr1 | grep -oe "-b [4,9,3,1][0-9]*" | sed 's/-b //g'`
		echo "$i baud is $inst_baud"
		sleep 5
		header=`sjinn -d ${inst_port} -b${inst_baud} -r1000 -s "\r\r\r64" -w4 | grep Time`
		#PREPARE A VARIABLE STRING CONSISTING IF THE INSTRUMENT HEADERS, SO ONE FOR ONE A VALUE IS ASSIGNED			
		prepped_headers=`echo "$header" | sed 's/(mg\/m3)//g' | sed 's/,/ /g' | sed 's/(m3)//g' | sed 's/PM2.5/PM25/g' |\
		sed 's/(KTS)//g' | sed 's/(DEG)//g' | sed 's/(C)//g' | sed 's/(%)//g' | sed 's/Time /Date Time /g' | sed 's/(KPH)//g' |\
		sed 's/(MPS)//g' | sed 's/(V)//g'`
		echo $prepped_headers
		cat $1 | sed 's/,/ /g' | while read `echo $prepped_headers`
		do
    	    #CALCULATE IF THE ERRORS ARE > 0, IF SO FLAG THAT HOUR
        	flag=`echo  ${E}+${U}+${M}+${L}+${R}+${N}+${F}+${P}+${D}+${C} | bc`
			if [[ $flag != 0 ]]; then
				op_code="5"
			else
				op_code="0"
			fi	
			#transpose the date and time by one hour
			old_date=`date -d "${Date} ${Time}" +%s`
			new_date=`echo $old_date-3600 | bc`
			new_date=`date -d @$new_date "+%m/%d/%Y %H:%M:00"`
				
			#DETERMINE THE DATA LABEL FOR EACH PARAMETER THIS INSTRUMENT REPORTS
        	#, THEN GRAB THE APPROPRIATE DMS LABEL TO REPORT IT BY, FROM
        	#THE dms.cfg FILE. 
			for d in `cat "resources/${i}/.display" | grep -oe 'PM10_[0-9a-z]=' -oe 'PM10s_[0-9a-z]=' -oe 'Q_ACT[_a-z0-9=]*' -oe 'PM2.5_[0-9a-z]=' -oe 'PMc_[0-9a-z]'`
			do
				dms_label=`grep "${d}" sys_resources/dms.cfg | awk '{ print $2 }'`
				case $d in
					PM10_[0-9a-z]=)
						this_value=`echo $PM10*1000 | bc`
            			#PUT THE PM10 DATA INTO THE .MIN FILE
 	             		echo "${saroad}|${dms_label}|${new_date}|${op_code}|$this_value|" >> $this_file
					;;
					PM10s_[0-9a-z]=)
						this_value=`echo $PM10s*1000 | bc`
						#PUT THE PM10s DATA INTO THE .MIN FILE
						echo "${saroad}|${dms_label}|${new_date}|${op_code}|$this_value|" >> $this_file
					;;
					Q_ACT[_a-z0-9=]*)
						#PUT THE Q TOTAL DATA INTO THE .MIN FILE
    	        		echo "${saroad}|${dms_label}|${new_date}|${op_code}|$Qt10|" >> $this_file
					;; 
					PM2.5_[0-9a-z]=)
						this_value=`echo $PM25*1000 | bc`
						#PUT THE PM10s DATA INTO THE .MIN FILE
						echo "${saroad}|${dms_label}|${new_date}|${op_code}|$this_value|" >> $this_file						
					;;
					PMc_[0-9a-z])
						this_value=`echo $PMc*1000 | bc`
						#PUT THE PM10s DATA INTO THE .MIN FILE
						echo "${saroad}|${dms_label}|${new_date}|${op_code}|$this_value|" >> $this_file
				esac
			done
		done
         
		echo "converting to windows....."
		#REPLACE CR WITH CR LF
	    sed -i 's/$/\r/g' $this_file
		#REMOVE BLANK LINES
    	sed -i 's/^$//g' $this_file
		echo "sending to dms...."
		bash sys_resources/carbput.sh "$this_file"
	    echo "COMPLETE...check DMS in about 15 minutes"
		exit
	fi
	
done
